package com.wr.controller;

import com.wr.config.FtpConfig;
import com.wr.util.FtpUtil;
import com.wr.utils.UploadUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author weirong
 * @Title:
 * @Description:
 * @date 2018/6/1215:08
 */
@RestController
public class ImageController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private FtpConfig ftpConfig;

    @RequestMapping(value="contentFileUpload",method= RequestMethod.POST)
    @ResponseBody
    public String contentFileUpload(MultipartFile picture, HttpServletRequest request) {

        if (picture!=null && picture.getOriginalFilename()!=null && picture.getOriginalFilename().trim().length()>0) {


            String oldName = picture.getOriginalFilename();// 获取图片原来的名字
            String picNewName = UploadUtils.generateRandonFileName(oldName);// 通过工具类产生新图片名称，防止重名
            String picSavePath = UploadUtils.generateRandomDir(picNewName);// 通过工具类把图片目录分级

            try {
                //图片上传服务器，返回地址
                return FtpUtil.pictureUploadByConfig(ftpConfig, picNewName, picSavePath, picture.getInputStream());// 上传到图片服务器的操
            } catch (IOException e) {
                logger.error(e,e);
            }

        }
        return "false";
    }
}
